package com.sam.im.heiqun.uis.activities;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.ValidateEntivity;
import com.sam.im.heiqun.nets.PGService;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;

import butterknife.BindView;
import butterknife.OnClick;

public class IdentityProvingActivity extends BaseSwipeBackActivity {

    private static final int TIME_SUB = 1000;


    @BindView(R.id.mobile)
    EditText mobile;
    @BindView(R.id.auth_code)
    EditText authCode;

    @BindView(R.id.get_auth_code)
    TextView getAuthCode;

    @BindView(R.id.next)
    Button next;

    private String moneyStr;
    private PGService mPGservice;
    private String netAuthCode;
    private String type;
    private String mobileCode = "+86";
    private int timeNum = 60;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case TIME_SUB:
                    timeNum--;
                    if (timeNum > 0) {
                        getAuthCode.setText(timeNum + "s");
                        handler.sendEmptyMessageDelayed(TIME_SUB, 1000);
                    } else {
                        getAuthCode.setText(R.string.get_auth_code);
                        getAuthCode.setTextColor(getResources().getColor(R.color.colorAccent));
                        timeNum = 60;
                        getAuthCode.setEnabled(true);
                    }
                    break;
            }
        }
    };

    @Override
    public int getContentViewId() {
        return R.layout.activity_identity_proving;
    }

    @Override
    protected String getTitleText() {
        mPGservice = PGService.getInstance();
        return getString(R.string.identity_proving);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        moneyStr = getIntent().getStringExtra("money");
        type = getIntent().getStringExtra("type");
    }

    @OnClick({R.id.next, R.id.get_auth_code})
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.next:
                doNext();
                finish();
                break;
            case R.id.get_auth_code:
                String mobileStr = mobile.getText().toString().trim();
                if (mobileStr.length() > 0) {
                    getAuthCode.setEnabled(false);
                    getAuthCode.setText(timeNum + "s");
                    getAuthCode.setTextColor(getResources().getColor(R.color.middle_gray_2));
                    handler.sendEmptyMessageDelayed(TIME_SUB, 1000);
                    getValidateNum(mobileCode +mobileStr);
                }
                break;
        }
    }

    private void doNext() {
        String nativeAuth = authCode.getText().toString().trim();
        if (nativeAuth!=null) {
            Intent intent = new Intent(this, SetPayPasswordActivity.class);
            intent.putExtra("mobile", mobile.getText().toString().trim());
            intent.putExtra("money", moneyStr);
            intent.putExtra("nativeAuth", nativeAuth);
//            showToast("type002="+type);
            intent.putExtra("type", type);
            startActivity(intent);
        } else {
            showToast(getString(R.string.auth_code_error));
        }
    }


    /**
     * 获取验证码
     */
    private void getValidateNum(String phoneNumber) {

        mPGservice.getValidateNum(phoneNumber).subscribe(new AbsAPICallback<ValidateEntivity>() {

            @Override
            public void onNext(ValidateEntivity s) {
                netAuthCode = s.getInfo() + "";
            }

            @Override
            protected void onResultError(ApiException ex) {
                if (ex.getCode() == -1) {
//                    showToast(getResources().getString(R.string.phoneiserror));
                    try {

                        com.alibaba.fastjson.JSONObject object = JSON.parseObject(ex.getDisplayMessage());
                        com.alibaba.fastjson.JSONObject data = object.getJSONObject("data");

                        showToast(data.getString("info"));
                        finish();
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                } else {
                    showToast(getResources().getString(R.string.net_visit_exception));
                }
            }
        });
    }
}
